package edu.bw.web.user;

import cn.hutool.json.JSONUtil;
import edu.bw.common.CommonResult;
import edu.bw.pojo.User;
import edu.bw.service.UserService;
import edu.bw.service.impl.UserServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet("/user/login")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端用户输入的账号密码
        String reqJSON = request.getReader().readLine();
        User user = JSONUtil.toBean(reqJSON, User.class);
        //2.调用service中的登录方法
        UserService userSevlet=new UserServiceImpl();
        Integer userId=userSevlet.userlogin(user);
        //3.向前端响应结果
        CommonResult result = CommonResult.ok().put("result", (userId != null) ? "登录成功" : "登录失败");

        response.getWriter().write(JSONUtil.toJsonStr(result));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
